home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / Table.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  2.8 KB  |  97 lines

  1. /*
  2. //   (C) COPYRIGHT International Business Machines Corp. 1993
  3. //   All Rights Reserved
  4. //   Licensed Materials - Property of IBM
  5. //   US Government Users Restricted Rights - Use, duplication or
  6. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. //
  8.  
  9. //
  10. // Permission to use, copy, modify, and distribute this software and its
  11. // documentation for any purpose and without fee is hereby granted, provided
  12. // that the above copyright notice appear in all copies and that both that
  13. // copyright notice and this permission notice appear in supporting
  14. // documentation, and that the name of I.B.M. not be used in advertising
  15. // or publicity pertaining to distribution of the software without specific,
  16. // written prior permission. I.B.M. makes no representations about the
  17. // suitability of this software for any purpose.  It is provided "as is"
  18. // without express or implied warranty.
  19. //
  20. // I.B.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I.B.M.
  22. // BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  23. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  24. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  25. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. //
  27. // Author:  John Spitzer, IBM AWS Graphics Systems (Austin)
  28. //
  29. */
  30. #ifdef WIN32
  31. #include <windows.h>
  32. #endif
  33. #include <string.h>
  34. #include "Table.h"
  35. #include "Global.h"
  36. #include <stdlib.h>
  37. #include <malloc.h>
  38.  
  39. TablePtr new_Table()
  40. {
  41.     TablePtr this = (TablePtr)malloc(sizeof(Table));
  42.     CheckMalloc(this);
  43.     this->num = 0;
  44.     return this;
  45. }
  46.  
  47. void delete_Table(TablePtr this)
  48. {
  49.     free(this);
  50. }
  51.  
  52. int Table__Load(TablePtr this, StringValuePtr load, int n)
  53. {
  54.     this->table = load;
  55.     this->num = n;
  56.     return True;
  57. }
  58.  
  59. #if defined(VACPP) && defined(__OS2__)
  60. #define BSEARCH_CALLBACK_TYPE _Optlink
  61. #else
  62. #define BSEARCH_CALLBACK_TYPE 
  63. #endif
  64.  
  65. int  BSEARCH_CALLBACK_TYPE StringValueCmp(const void* arg1, const void* arg2)
  66. {
  67.     return strcmp(*((char**)arg1),*((char**)arg2));
  68. }
  69.  
  70. int Table__Lookup(TablePtr this, const char* string, int* value)
  71. {
  72.     StringValuePtr found;
  73.  
  74.     found = (StringValue*)bsearch(&string, this->table, (size_t)(this->num), 
  75.                   sizeof(StringValue), StringValueCmp);
  76.     if (found) {
  77.     *value = found->value;
  78.     return True;
  79.     } else {
  80.     return False;
  81.     }
  82. }
  83.  
  84. int Table__InverseLookup(TablePtr this, char* string, int value)
  85. {
  86.     /* This will only be done when errors occur, so a slow linear */
  87.     /* search will suffice.                                       */
  88.     int i;
  89.     for (i=0; i<this->num && value != this->table[i].value; i++);
  90.     if (i<this->num) {
  91.     strcpy(string, this->table[i].string);
  92.     return True;
  93.     } else {
  94.     return False;
  95.     }
  96. }
  97.